Factory Pattern
Why this matters in interviews: almost every LLD problem has a "which concrete class do I create?" moment: the vehicle type in a parking lot, the payment provider at checkout, the notification channel, the chess piece, the vending machine state. Interviewers watch whether creation logic is centralized and closed for modification or scattered as
switchstatements across the codebase. They also ask the classic clarification: "What's the difference between Factory Method and Abstract Factory?"
Prerequisites: OOP Pillars (polymorphism), SOLID Principles (OCP, DIP). Examples are in TypeScript. For Node.js backend examples (DB client, report generator, queue jobs) see Factory Pattern in JS.
1. The problem factories solve
// OrderService.ts: business logic that knows every concrete class
class OrderService {
notify(order: Order, channel: string) {
let n;
if (channel === 'email') n = new EmailNotification(/* SMTP config */);
else if (channel === 'sms') n = new SmsNotification(/* Twilio keys */);
else if (channel === 'push') n = new PushNotification(/* FCM creds */);
n.send(order.userId, 'Order placed');
}
}
What's wrong with this:
- OCP violation: adding WhatsApp means editing
OrderService, and every other place that has the sameifchain. - DIP violation: high-level business logic depends on low-level concrete classes and their configuration.
- Duplication: the same construction knowledge (credentials, defaults) is copied around.
- Hard to test: you can't substitute a fake channel without editing the code.
The core idea of every factory variant: move the decision "which concrete class, and how to build it" into one place, and let clients depend on an interface.
2. The family at a glance
| Variant | GoF? | Mechanism | Creates | Use when |
|---|---|---|---|---|
| Simple Factory | No (an idiom) | One function or class with a switch or map | One product, chosen by a parameter | The type is decided at runtime from input or config. The most common in practice. |
| Factory Method | Yes | Inheritance: a subclass overrides createX() | One product per subclass | A base class owns a workflow, and subclasses decide which product it uses |
| Abstract Factory | Yes | Composition: an object with several createX() methods | A family of related products | Products must be used together and stay consistent (AWS storage + AWS queue) |
| Static factory method | No (Effective Java, Item 1) | A named static method instead of a constructor | An instance of the class (or a subtype) | You want descriptive names, caching or validation, or to return subtypes |
All of them share one interface for the products:
interface Notification {
send(to: string, message: string): string;
}
class EmailNotification implements Notification {
send(to: string, message: string) { return `email:${to}:${message}`; }
}
class SmsNotification implements Notification {
send(to: string, message: string) { return `sms:${to}:${message}`; }
}
class PushNotification implements Notification {
send(to: string, message: string) { return `push:${to}:${message}`; }
}
3. Simple Factory
a) The switch version, made type-safe
type Channel = 'email' | 'sms' | 'push';
function createNotification(channel: Channel): Notification {
switch (channel) {
case 'email': return new EmailNotification();
case 'sms': return new SmsNotification();
case 'push': return new PushNotification();
default: {
const unreachable: never = channel; // compile error if a new Channel isn't handled
throw new Error(`Unknown channel: ${unreachable}`);
}
}
}
createNotification('sms').send('a', 'hi'); // "sms:a:hi"
The never check is a nice touch to show: add 'whatsapp' to Channel and the compiler points straight at the factory.
- Pros: creation lives in one place, and clients depend only on
Notification. - Cons: the factory itself still has to be edited for every new type, so it isn't strictly open/closed.
b) The registry version: open for extension
type NotificationCtor = new () => Notification;
class NotificationFactory {
private readonly registry = new Map<string, NotificationCtor>();
register(channel: string, ctor: NotificationCtor): this {
if (this.registry.has(channel)) throw new Error(`Channel already registered: ${channel}`);
this.registry.set(channel, ctor);
return this;
}
create(channel: string): Notification {
const Ctor = this.registry.get(channel);
if (!Ctor) throw new Error(`Unknown channel: ${channel}`);
return new Ctor();
}
}
const factory = new NotificationFactory()
.register('email', EmailNotification)
.register('sms', SmsNotification);
// A new module adds a channel without touching the factory's code:
factory.register('whatsapp', WhatsAppNotification);
factory.create('whatsapp').send('b', 'yo');
- Pros: truly OCP: plugins register themselves. It's how many frameworks resolve handlers, serializers and drivers.
- Cons: you lose the compile-time exhaustiveness check (the keys are strings), and a missing registration only shows up at runtime. Fail fast on startup, and reject duplicates.
If products need different constructor arguments, register creator functions instead of classes: Map<string, (cfg: Config) => Notification>.
4. Factory Method (GoF)
Define an interface for creating an object, but let subclasses decide which class to instantiate.
The base class contains a workflow that needs a product. It calls an abstract createX() (the factory method), and each subclass supplies the concrete product.
interface Document {
render(): string;
}
class PdfDocument implements Document { render() { return 'PDF'; } }
class HtmlDocument implements Document { render() { return 'HTML'; } }
abstract class ReportExporter {
// the factory method
protected abstract createDocument(): Document;
// the shared workflow that uses it (often a Template Method)
export(title: string): string {
const doc = this.createDocument();
return `[${title}] ${doc.render()}`;
}
}
class PdfExporter extends ReportExporter {
protected override createDocument(): Document { return new PdfDocument(); }
}
class HtmlExporter extends ReportExporter {
protected override createDocument(): Document { return new HtmlDocument(); }
}
new PdfExporter().export('Q3'); // "[Q3] PDF"
new HtmlExporter().export('Q3'); // "[Q3] HTML"
ReportExporter (abstract) Document (interface)
+ export() ──calls──▶ createDocument() ▲
▲ │
┌─────┴──────┐ ┌─────────┴────────┐
PdfExporter HtmlExporter PdfDocument HtmlDocument
createDocument() → new PdfDocument / new HtmlDocument
- Why it works:
export()is written once against theDocumentinterface. Adding Excel means addingExcelExporterandExcelDocument, with no edits to existing code. - When it's preferable: you have a framework or base class with a fixed algorithm and want subclasses to plug in one varying object.
- Limitations: a parallel class hierarchy (one creator subclass per product), and it's inheritance-based, so you can't switch the product at runtime. When the workflow isn't shared, a simple factory or plain dependency injection is lighter.
Real-world examples: Iterable.iterator() in Java (each collection decides which iterator to create), JS [Symbol.iterator](), and Thread/Executor thread factories.
5. Abstract Factory (GoF)
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
The key word is families: products that must be used together. Mixing them would be a bug (AWS S3 storage with GCP Pub/Sub notifications, a dark-theme button with a light-theme dialog).
interface BlobStorage { put(key: string, data: string): string }
interface MessageQueue { publish(topic: string, msg: string): string }
interface CloudFactory { // the abstract factory: one method per product
createStorage(): BlobStorage;
createQueue(): MessageQueue;
}
class S3Storage implements BlobStorage { put(k: string) { return `s3://${k}`; } }
class SqsQueue implements MessageQueue { publish(t: string) { return `sqs:${t}`; } }
class GcsStorage implements BlobStorage { put(k: string) { return `gs://${k}`; } }
class PubSubQueue implements MessageQueue { publish(t: string) { return `pubsub:${t}`; } }
class AwsFactory implements CloudFactory { // concrete factory = one consistent family
createStorage() { return new S3Storage(); }
createQueue() { return new SqsQueue(); }
}
class GcpFactory implements CloudFactory {
createStorage() { return new GcsStorage(); }
createQueue() { return new PubSubQueue(); }
}
class UploadService {
private readonly storage: BlobStorage;
private readonly queue: MessageQueue;
constructor(cloud: CloudFactory) { // the client only knows the abstract factory
this.storage = cloud.createStorage();
this.queue = cloud.createQueue();
}
upload(key: string, data: string): string[] {
return [this.storage.put(key, data), this.queue.publish('uploaded', key)];
}
}
new UploadService(new AwsFactory()).upload('a.png', 'x'); // ["s3://a.png", "sqs:uploaded"]
new UploadService(new GcpFactory()).upload('a.png', 'x'); // ["gs://a.png", "pubsub:uploaded"]
- Why it works: choosing the factory once (at the composition root, from config) guarantees every product comes from the same family. Inconsistent combinations become impossible.
- When it's preferable: multi-cloud or multi-vendor adapters, UI themes and toolkits (light/dark, iOS/Android widgets), database drivers (a connection creates matching statements and transactions), and test vs production families (fake storage + fake queue).
- Limitations: adding a new product type (say a
createCache()) means changing the interface and every concrete factory. It's easy to add a new family, hard to add a new product. It's also heavyweight when there's only one product. That's just a factory method or simple factory.
Factory Method vs Abstract Factory (the most-asked question)
| Factory Method | Abstract Factory | |
|---|---|---|
| Mechanism | Inheritance: subclasses override one method | Composition: you're handed a factory object |
| Creates | One product | A family of related products |
| The variation lives in | The creator subclass | The concrete factory object |
| Typical shape | abstract createDocument() inside a class with other logic | An interface made only of createA(), createB(), ... |
| Relationship | The methods of an Abstract Factory are often implemented as factory methods |
One-liner: "Factory Method lets a subclass pick one product. Abstract Factory is an object that produces a matching set of products."
6. Static factory methods (named constructors)
class Money {
private static readonly ZERO_CACHE = new Map<string, Money>();
private constructor(readonly cents: number, readonly currency: string) {}
static ofCents(cents: number, currency: string): Money {
if (!Number.isInteger(cents)) throw new Error('cents must be an integer');
if (cents === 0) return Money.zero(currency); // can return a cached instance
return new Money(cents, currency);
}
static of(amount: number, currency: string): Money {
return Money.ofCents(Math.round(amount * 100), currency); // intent-revealing name
}
static zero(currency: string): Money {
let z = Money.ZERO_CACHE.get(currency);
if (!z) { z = new Money(0, currency); Money.ZERO_CACHE.set(currency, z); }
return z;
}
}
Money.of(12.34, 'USD').cents; // 1234
Money.of(0.1 + 0.2, 'USD').cents; // 30, because rounding hides float error
Money.zero('USD') === Money.ofCents(0, 'USD'); // true (cached)
Advantages over constructors (Effective Java, Item 1):
- Names:
Money.of(...),Money.zero(),Duration.ofMinutes(5). - Can return cached or shared instances (
Integer.valueOfcaches -128..127,Boolean.valueOf). - Can return a subtype or a hidden implementation class (Java's
EnumSet.ofreturns aRegularEnumSetor aJumboEnumSetdepending on how many constants the enum type has, and callers never know). - Can validate or normalize before construction, and keep the constructor private.
Real-world examples: Array.from, Array.of, Promise.resolve, Object.create, document.createElement('div') (a simple factory keyed by tag name), Node's crypto.createHash('sha256'), React.createElement, Java's List.of / Optional.of.
Async factories: when construction needs I/O
A constructor can't be async in JS/TS (or in most languages). If an object isn't usable until it has connected, loaded a file or fetched a key, the usual mistakes are an init() method callers forget to call, or kicking off a Promise in the constructor that nobody awaits. Use a static async factory with a private constructor instead:
interface Secrets { apiKey: string }
class PaymentsClient {
private constructor(private readonly secrets: Secrets) {} // only receives ready-to-use state
static async create(loadSecrets: () => Promise<Secrets>): Promise<PaymentsClient> {
const secrets = await loadSecrets(); // the I/O happens here
if (!secrets.apiKey) throw new Error('missing apiKey'); // validate before the object exists
return new PaymentsClient(secrets);
}
charge(cents: number): string {
return `charged ${cents} with key ${this.secrets.apiKey.slice(0, 3)}…`;
}
}
const client = await PaymentsClient.create(async () => ({ apiKey: 'sk_live_123' }));
client.charge(500); // "charged 500 with key sk_…"
- Why it works: a
PaymentsClientcan only be obtained after its async setup succeeded, so there's no "not initialized yet" state to check in every method. - Combine it with the singleton Promise trick (cache the Promise returned by
create()) when the client should be shared. See Singleton, section 4. - Seen in the wild:
mongodb's staticMongoClient.connect(url), Playwright'schromium.launch(), andfs.promises.open()(resolves to a readyFileHandle).
7. Factories in LLD problems
Parking lot: create the vehicle from its type
enum VehicleType { Bike = 'BIKE', Car = 'CAR', Truck = 'TRUCK' }
enum SpotSize { Small = 1, Medium = 2, Large = 3 }
abstract class Vehicle {
constructor(readonly plate: string) {}
abstract readonly type: VehicleType;
abstract readonly requiredSpot: SpotSize;
}
class Bike extends Vehicle { readonly type = VehicleType.Bike; readonly requiredSpot = SpotSize.Small; }
class Car extends Vehicle { readonly type = VehicleType.Car; readonly requiredSpot = SpotSize.Medium; }
class Truck extends Vehicle { readonly type = VehicleType.Truck; readonly requiredSpot = SpotSize.Large; }
// Record<VehicleType, ...> makes the compiler insist every enum value has a constructor
const vehicleCtors: Record<VehicleType, new (plate: string) => Vehicle> = {
[VehicleType.Bike]: Bike,
[VehicleType.Car]: Car,
[VehicleType.Truck]: Truck,
};
function createVehicle(type: VehicleType, plate: string): Vehicle {
return new vehicleCtors[type](plate);
}
createVehicle(VehicleType.Truck, 'KA01').requiredSpot; // SpotSize.Large
Record<VehicleType, ...> gives you the registry's openness and compile-time exhaustiveness.
Other common LLD spots
| Problem | Factory creates | Variant |
|---|---|---|
| Payment / checkout | PaymentGateway (Stripe, PayPal, Razorpay) per provider | Simple factory / registry |
| Notification service | Notification channel per user preference | Registry |
| Chess / board games | Piece from a symbol ('K', 'Q') | Simple factory |
| Vending machine / ATM | State objects | Simple factory (often with cached instances) |
| Cross-platform UI | Button + Menu + Dialog per OS or theme | Abstract factory |
| File parser / importer | Parser per file extension | Registry |
| Logger / exporter frameworks | Log sink or document per subclass | Factory method |
Inject the factory so business logic stays testable
interface PaymentGateway { charge(cents: number): string }
type GatewayFactory = (provider: string) => PaymentGateway;
class CheckoutService {
constructor(private readonly gatewayFor: GatewayFactory) {}
pay(provider: string, cents: number): string {
return this.gatewayFor(provider).charge(cents);
}
}
// in a test: a fake factory that records calls
const calls: string[] = [];
const fakeFactory: GatewayFactory = (p) => ({ charge: (c) => { calls.push(`${p}:${c}`); return 'ok'; } });
new CheckoutService(fakeFactory).pay('stripe', 500); // "ok", and calls = ["stripe:500"]
In TS, a factory is often just a function type. You don't need a FactoryFactory class.
8. Factory vs related patterns
| Pattern | Question it answers | Relationship to Factory |
|---|---|---|
| Strategy | How should this be done? (swap the behaviour) | A factory often picks the strategy: const s = factory.create(type); s.execute() |
| Builder | How do I assemble one complex object step by step? | Factory picks which class in one call. Builder configures how one object is assembled. See Builder. |
| Singleton | How many instances? | Factories (and their registries) are often single instances. Factories can also return cached singletons. See Singleton. |
| Prototype | Create by cloning an existing object | An alternative to factories when configuring from scratch is expensive |
| Dependency injection | Who wires objects together? | A DI container is essentially a big, configurable factory. Use factories for objects created at runtime per request, and DI for the long-lived graph. |
9. The same idea in other interview languages
| Language | Idiomatic factory | Notes |
|---|---|---|
| Java | Static factories (List.of, Optional.ofNullable), Map<String, Supplier<Shape>> registries, ServiceLoader for plugins | Enum-keyed EnumMap<Type, Supplier<T>> gives exhaustiveness. Spring's @Bean methods are factory methods. |
| Python | @classmethod alternate constructors (datetime.fromtimestamp, dict.fromkeys), and a plain dict from key to class | Classes are first-class, so REGISTRY[kind](**cfg) is a complete simple factory. Decorators often do the registering: @register("email"). |
| Go | NewX(...) constructor functions, since Go has no constructors | Go's convention is "accept interfaces, return structs": NewServer returns *Server, not an interface. Return an interface only when you really are choosing among implementations (a true factory). |
| C++ | Functions returning std::unique_ptr<Base> | Ownership is explicit in the return type, so the caller owns the product. |
# Python registry factory with a decorator
_REGISTRY: dict[str, type] = {}
def register(kind: str):
def deco(cls):
_REGISTRY[kind] = cls
return cls
return deco
@register("email")
class EmailNotification:
def send(self, to, msg): return f"email:{to}:{msg}"
def create_notification(kind: str):
try:
return _REGISTRY[kind]()
except KeyError:
raise ValueError(f"unknown channel: {kind}") from None
10. Pitfalls
- God factory: one
createAnything(type)with 40 branches. Split it per product family. - Returning concrete types (
createNotification(): EmailNotification), which defeats the point. Return the interface. - Factories for one class: if there's only ever
new UserService(), a factory adds indirection without benefit. - Hidden configuration: a factory that reads
process.envdeep inside makes behaviour hard to trace. Pass config in. - Silent fallbacks:
default: return new EmailNotification()hides typos. Throw on unknown types. - Confusing Factory with Builder: if the caller is passing 10 options into
create(...), you probably want a builder (or an options object). - Async setup in a constructor: a constructor that starts I/O without awaiting it hands out half-initialized objects. Use an async factory.
11. How to talk about it in the room
When you spot a "which concrete class?" decision in an LLD problem:
- Name the variation point. "Payment providers vary, and we'll add more."
- Define the product interface first (
PaymentGateway.charge()), so business logic depends only on it. - Pick the lightest variant that fits, and say why:
- The type comes from input or config → simple factory, as a registry if new types arrive often.
- A base class's workflow needs a subclass-specific object → factory method.
- Several products must match (same vendor, same theme) → abstract factory.
- Inject the factory into the service, so tests can pass a fake.
- Mention the trade-off: a
switchfactory isn't fully open/closed, but it's contained and exhaustiveness-checked. A registry is open but only checked at runtime, so fail fast at startup.
Don't draw an abstract factory for a single product, and don't call a switch function "the Factory Method pattern". Interviewers notice both.
Interview questions (with crisp answers)
Q: Factory Method vs Abstract Factory? Factory Method uses inheritance so a subclass decides one product. Abstract Factory is an object that creates a family of related products, keeping them consistent.
Q: Is Simple Factory a GoF pattern? No. It's an idiom. GoF defines Factory Method and Abstract Factory. Simple Factory is still the most used.
Q: How does a factory support SOLID? DIP: clients depend on interfaces, not concrete classes. OCP: new types are added by registering, not by editing clients (fully OCP with a registry). SRP: construction knowledge lives in one place.
Q: A switch in a factory still violates OCP. Is that bad?
It's contained in one place, which is a big improvement over scattered switch statements. If types are added often or by third parties, use a registry. In TS, the never exhaustiveness check makes the switch safe to extend.
Q: Factory vs Strategy? Factory is about creating the right object, and Strategy is about behaviour that's swappable at runtime. They're usually combined: the factory returns a strategy.
Q: Factory vs Builder? Factory: "which implementation?", usually one call. Builder: "how do I assemble this complex object?", step by step with validation.
Q: When is Abstract Factory overkill? When there's a single product type, or families never need to be swapped. Adding a new product type to an abstract factory forces changes to every concrete factory.
Common mistakes
- Answering "Factory Method" when you mean a simple factory function. Know the difference.
- Exposing concrete classes from the factory's return type.
- Unknown type gives a silent default instead of an error.
- Building an Abstract Factory for a single product.
- Using a factory to set many optional fields, which is a job for a Builder.